/*
 * 
 */
package fr.univtln.superplanning.gui.event;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fr.univtln.superplanning.controller.EventController;
import fr.univtln.superplanning.gui.LayoutConfiguration;
import fr.univtln.superplanning.gui.LookAndFeelManager;

/**
 * This class is the view for editing a event (course).
 * 
 * @author vincent
 * 
 */
public class EventEditor extends JFrame implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox promotionsComboBox;
	private JTextField dateTextField;
	private JTextField timeTextField;
	private JTextField durationTextField;

	private JComboBox teachingComboBox;
	private JComboBox teacherComboBox;
	private JComboBox eventTypeComboBox;

	private JButton validateFirstElementsButton;

	private JComboBox classroomComboBox;

	private JButton validateEventButton;
	private JButton cancelEventButton;
	private JButton deleteCourseButton;

	// The controller
	private EventController eventController;

	public static void main(String[] args) {
		LookAndFeelManager.loadSystemLookAndFeel();
		// EventController eventController = new EventController(new
		// SuperplanningManagerDB());
		// eventController.showView();

	}

	public EventEditor(EventController eventController) {
		this.eventController = eventController;
		initComponents();
	}

	private void initComponents() {

		// TODO: JAGO : faut que tu mettes les bundles ici ;)

		GridBagConstraints constraints = new GridBagConstraints();
		setLayout(new GridBagLayout());

		// Window configuration

		promotionsComboBox = new JComboBox();
		dateTextField = new JTextField();
		timeTextField = new JTextField();
		durationTextField = new JTextField();

		teachingComboBox = new JComboBox();
		teacherComboBox = new JComboBox();
		eventTypeComboBox = new JComboBox();

		validateFirstElementsButton = new JButton("OK");

		classroomComboBox = new JComboBox();

		validateEventButton = new JButton("Valider");
		cancelEventButton = new JButton("Annuler");
		deleteCourseButton = new JButton("Supprimer");

		// Layout configuration
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.insets = LayoutConfiguration.INSETS;
		constraints.weightx = 0;
		constraints.fill = GridBagConstraints.HORIZONTAL; // horizontal stretch

		constraints.gridwidth = 2;
		constraints.anchor = GridBagConstraints.CENTER;
		JLabel titleLabel = new JLabel("Programmation d'un cours");
		titleLabel.setFont(new Font("Courier", Font.ITALIC, 20));
		add(titleLabel, constraints);

		constraints.gridy++;
		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		add(new JLabel("Promotion"), constraints);
		constraints.gridx++;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		add(promotionsComboBox, constraints);

		constraints.gridy++;
		constraints.gridx = 0;

		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		add(new JLabel("Date"), constraints);
		constraints.gridx++;
		add(dateTextField, constraints);

		constraints.gridx = 0;
		constraints.gridy++;
		add(new JLabel("Heure"), constraints);
		constraints.gridx++;
		add(timeTextField, constraints);

		constraints.gridx = 0;
		constraints.gridy++;
		add(new JLabel("Durée"), constraints);
		constraints.gridx++;
		add(durationTextField, constraints);

		constraints.gridy++;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		add(new JLabel("Enseignement"), constraints);

		constraints.gridx++;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		add(teachingComboBox, constraints);

		constraints.gridy++;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		add(new JLabel("Type de cours"), constraints);

		constraints.gridx++;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		add(eventTypeComboBox, constraints);

		constraints.gridy++;
		constraints.gridx = 0;

		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		add(new JLabel("Enseignant"), constraints);
		constraints.gridx++;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		add(teacherComboBox, constraints);

		constraints.gridy++;
		constraints.weighty = 0;
		constraints.fill = GridBagConstraints.NONE;

		add(validateFirstElementsButton, constraints);

		constraints.gridy++;
		constraints.gridx = 0;

		constraints.anchor = GridBagConstraints.LINE_START;
		constraints.gridwidth = GridBagConstraints.RELATIVE;
		constraints.fill = GridBagConstraints.HORIZONTAL;

		add(new JLabel("Salle"), constraints);
		constraints.gridx++;
		constraints.anchor = GridBagConstraints.LINE_END;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		add(classroomComboBox, constraints);

		constraints.gridy++;
		constraints.gridx = 1;

		constraints.weighty = 0;
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.LAST_LINE_END;
		add(initButtonsPanel(), constraints);

		displayWindow();

	}

	public void displayWindow() {
		// pack to set the good size.
		pack();
		setResizable(false);

		Dimension screenDimension = getToolkit().getScreenSize();

		// place the frame on the center of the window.
		setLocation(screenDimension.width / 2 - getWidth() / 2, screenDimension.height / 2
				- getHeight() / 2);

		setVisible(true);
	}

	/**
	 * This method will init the buttons panel
	 * 
	 * @return
	 */
	public JComponent initButtonsPanel() {
		JPanel buttonsPanel = new JPanel();

		buttonsPanel.add(cancelEventButton);
		buttonsPanel.add(deleteCourseButton);
		buttonsPanel.add(validateEventButton);

		return buttonsPanel;
	}

	public JComponent buildEventDateTimePanel() {
		JPanel eventDateTimePanel = new JPanel();

		eventDateTimePanel.add(new JLabel("Date"));
		eventDateTimePanel.add(dateTextField);
		eventDateTimePanel.add(new JLabel("Heure"));
		eventDateTimePanel.add(timeTextField);

		return eventDateTimePanel;
	}

	public JComboBox getPromotionsComboBox() {
		return promotionsComboBox;
	}

	public void setPromotionsComboBox(JComboBox promotionsComboBox) {
		this.promotionsComboBox = promotionsComboBox;
	}

	public JTextField getDateTextField() {
		return dateTextField;
	}

	public void setDateTextField(JTextField dateTextField) {
		this.dateTextField = dateTextField;
	}

	public JTextField getTimeTextField() {
		return timeTextField;
	}

	public void setTimeTextField(JTextField timeTextField) {
		this.timeTextField = timeTextField;
	}

	public JTextField getDurationTextField() {
		return durationTextField;
	}

	public void setDurationTextField(JTextField durationTextField) {
		this.durationTextField = durationTextField;
	}

	public JComboBox getTeachingComboBox() {
		return teachingComboBox;
	}

	public void setTeachingComboBox(JComboBox teachingComboBox) {
		this.teachingComboBox = teachingComboBox;
	}

	public JComboBox getTeacherComboBox() {
		return teacherComboBox;
	}

	public void setTeacherComboBox(JComboBox teacherComboBox) {
		this.teacherComboBox = teacherComboBox;
	}

	public JButton getValidateFirstElementsButton() {
		return validateFirstElementsButton;
	}

	public void setValidateFirstElementsButton(JButton validateFirstElementsButton) {
		this.validateFirstElementsButton = validateFirstElementsButton;
	}

	public JComboBox getClassroomComboBox() {
		return classroomComboBox;
	}

	public void setClassroomComboBox(JComboBox classroomComboBox) {
		this.classroomComboBox = classroomComboBox;
	}

	public JButton getValidateEventButton() {
		return validateEventButton;
	}

	public void setValidateEventButton(JButton validateEventButton) {
		this.validateEventButton = validateEventButton;
	}

	public JButton getCancelEventButton() {
		return cancelEventButton;
	}

	public void setCancelEventButton(JButton cancelEventButton) {
		this.cancelEventButton = cancelEventButton;
	}

	public JButton getDeleteCourseButton() {
		return deleteCourseButton;
	}

	public void setDeleteCourseButton(JButton deleteCourseButton) {
		this.deleteCourseButton = deleteCourseButton;
	}

	public EventController getEventController() {
		return eventController;
	}

	public JComboBox getEventTypeComboBox() {
		return eventTypeComboBox;
	}

	public void setEventTypeComboBox(JComboBox eventTypeComboBox) {
		this.eventTypeComboBox = eventTypeComboBox;
	}

	public void setEventController(EventController eventController) {
		this.eventController = eventController;
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

}
